//
//  ViewController.swift
//  testTableView
//
//  Created by TD Lemon on 2018/6/15.
//  Copyright © 2018 sunshinfight. All rights reserved.
//

import UIKit

class ViewController: UITableViewController {
    private let cellId = "MainListTableViewCell"
    
    private var isAppear = false
    let v = CellTopView()
    let image: UIImageView = {
       let img = UIImageView()
        img.image = #imageLiteral(resourceName: "bgImage")
        return img
    }()
    private var topViewHight = UIScreen.main.bounds.width*(9/16)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        v.frame.size = CGSize(width: tableView.frame.size.width, height: topViewHight)
        image.frame.size = CGSize(width: tableView.frame.size.width, height: topViewHight)
        tableView.tableHeaderView = image
//        tableView.addSubview(image)
//        tableView.bringSubview(toFront: v)
        
//        v.topAnchor.constraint(equalTo: tableView.topAnchor).isActive = true
//        v.leftAnchor.constraint(equalTo: tableView.leftAnchor).isActive = true
//        v.rightAnchor.constraint(equalTo: tableView.rightAnchor).isActive = true
//        v.frame.size.height = topViewHight
//        tableView.estimatedRowHeight = 100
        
//        tableView.contentInset = UIEdgeInsets(top: topViewHight, left: 0, bottom: 0, right: 0)
        tableView.backgroundColor = .red
        
        tableView.tableFooterView = UIView()
        
        tableView.contentInsetAdjustmentBehavior = .never
        
        
        
        navigationController?.navigationBar.shadowImage = UIImage()
        
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        isAppear = true
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        isAppear = false
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
    }
    

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0{
            return "section 1"
        }
        return nil
    }
    
    
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if isAppear{
            let offsetY = scrollView.contentOffset.y
            print(scrollView.contentOffset.y)
            if offsetY > 0, offsetY <= 150{
                tableView.contentInset.top = -scrollView.contentOffset.y
            }
            
            
            
//            if scrollView.contentOffset.y < -62{
//                tableView.contentInset.top = -scrollView.contentOffset.y
//            }
//
//            if scrollView.contentOffset.y < -topViewHight{
//                tableView.contentInset.top = topViewHight
//            }
            
            
//            if tableView.contentInset.top > 50{
//                tableView.contentInset.top = 0
//            }else if tableView.contentInset.top < 50, tableView.contentInset.top > 0{
//                tableView.contentInset.top = 100
//            }
        }
    }
    
    override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
//        if scrollView.contentOffset.y < -50{
//            tableView.contentInset.top = 0
//        }else if scrollView.contentOffset.y > 50 {
//            tableView.contentInset.top = 100
//        }
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! MainListTableViewCell
        
        return cell
    }


}

